package com.example.everying.controller.system;/*
 * 项目名称: everything
 * 文件名: VersionControlController
 * 作者: 张泽阳
 * 日期: 2025/7/19
 * 描述:
 */

import com.example.everying.annotation.WebLog;
import com.example.everying.context.UserContext;
import com.example.everying.model.dto.system.CheckVersionDto;
import com.example.everying.model.dto.system.VersionPageDto;
import com.example.everying.model.entity.system.SysVersionControl;
import com.example.everying.model.vo.system.UploadApkVo;
import com.example.everying.model.vo.system.UploadWgtVo;
import com.example.everying.service.system.SysVersionControlService;
import com.example.everying.utils.Result;
import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;

@Api(tags = "版本控制")
@RestController
@RequestMapping("/system/versionControl")
public class SysVersionControlController {

    @Value("${apkPath}")
    private String apkPath;

    @Value("${apkUrlPrefix}")
    private String apkUrlPrefix;

    @Value("${wgtPath}")
    private String wgtPath;

    @Value("${wgtUrlPrefix}")
    private String wgtUrlPrefix;

    @Autowired
    private SysVersionControlService versionControlService;

    @WebLog(description = "新增安装包")
    @PostMapping("/add")
    public Result<?> add(@RequestBody SysVersionControl versionControl){
        return Result.ok(versionControlService.add(versionControl));
    }

    @WebLog(description = "后台版本列表")
    @PostMapping("/list")
    public Result<?> list(@RequestBody VersionPageDto dto){
        return versionControlService.getList(dto);
    }

    @WebLog(description = "检查版本")
    @PostMapping("/checkVersion")
    public Result<?> checkVersion( @RequestBody CheckVersionDto dto){
        return versionControlService.checkVersion(dto);
    }



    @WebLog(description = "上传apk安装包")
    @PostMapping("/uploadApk")
    public Result<?> uploadApk(@RequestParam("file")MultipartFile  file) {
        if (UserContext.getUser().getIsAdministrator() != 1){
            return Result.fail("权限不足");
        }
        if (file == null){
            return Result.fail("文件不能为空");
        }
        // 获取文件后缀
        String originalFilename = file.getOriginalFilename();
        String suffix = "";
        if (originalFilename != null && originalFilename.contains(".")) {
            suffix = originalFilename.substring(originalFilename.lastIndexOf(".")).toLowerCase();
        }
        // 判断文件后缀只能是.apk 或 .wgt
        if (!".apk".equals(suffix) && !".wgt".equals(suffix)) {
            return Result.fail("只能上传 apk 文件");
        }
        try {
            Path uploadPath = Paths.get(apkPath);
            if (!Files.exists(uploadPath)){
                Files.createDirectories(uploadPath);
            }
            String fileName = "shibao.apk";
            Path filePath = uploadPath.resolve(fileName);
            Files.copy(file.getInputStream(),filePath,java.nio.file.StandardCopyOption.REPLACE_EXISTING);
            UploadApkVo uploadApkVo = new UploadApkVo();
            uploadApkVo.setApkPath(filePath.toString());
            uploadApkVo.setApkPackageName(fileName);
            uploadApkVo.setApkDownloadPath(apkUrlPrefix + fileName);
            return Result.ok(uploadApkVo);

        } catch (IOException e) {
            e.printStackTrace();
            return Result.fail("上传文件失败");
        }
    }

    @WebLog(description = "上传wgt安装包")
    @PostMapping("/uploadWgt")
    public Result<?> uploadWgt(@RequestParam("file")MultipartFile  file) {
        if (UserContext.getUser().getIsAdministrator() != 1){
            return Result.fail("权限不足");
        }
        if (file == null){
            return Result.fail("文件不能为空");
        }
        // 获取文件后缀
        String originalFilename = file.getOriginalFilename();
        String suffix = "";
        if (originalFilename != null && originalFilename.contains(".")) {
            suffix = originalFilename.substring(originalFilename.lastIndexOf(".")).toLowerCase();
        }
        // 判断文件后缀只能是.apk 或 .wgt
        if (!".wgt".equals(suffix)) {
            return Result.fail("只能上传  wgt 文件");
        }
        try {
            Path uploadPath = Paths.get(wgtPath);
            if (!Files.exists(uploadPath)){
                Files.createDirectories(uploadPath);
            }
            String fileName = "shibao.wgt";
            Path filePath = uploadPath.resolve(fileName);
//            if(Files.exists(filePath)){
//                return Result.fail("文件已存在");
//            }
            Files.copy(file.getInputStream(),filePath, java.nio.file.StandardCopyOption.REPLACE_EXISTING);
            UploadWgtVo uploadWgtVo = new UploadWgtVo();
            uploadWgtVo.setWgtDownloadPath(wgtUrlPrefix + fileName);
            uploadWgtVo.setWgtPath(filePath.toString());
            uploadWgtVo.setWgtPackageName(fileName);
            return Result.ok(uploadWgtVo);

        } catch (IOException e) {
            e.printStackTrace();
            return Result.fail("上传文件失败");
        }
    }

}
